var util={
		el:{
			success:function(title,callback){
				new PNotify({
	                title: '操作提示',
	                text: title,
	                type: 'success',
	                styling: 'bootstrap3'
	            });
				if(callback){
					setTimeout(function(){
						callback();
					},3);
				}
			},
			error:function(title){
				new PNotify({
	                title: '操作提示',
	                text: title,
	                type: 'error',
	                styling: 'bootstrap3'
	            });
			},
			confirm:function(title,callback){
				$("#js-modal-dialog .modal-body").html(title);
				$("#js-btn-modal-dialog").click();
				//确认
				$("#js-modal-dialog .confirm-ok").one("click",function(){
					if(callback){
						callback();
						//关闭
						$("#js-modal-dialog").click();
					}	
				});
			}
		},
		str:{
			//格式化输出fmt("{0}xxx{1}xxx",1,2)=>1xxx2xxx
			fmt:function(str,data){
				if(!str){
					return str;
				}
				//数据为数组时，转为多参数调用
				if(data instanceof Array){
					data.unshift(str);
					return arguments.callee.apply(this,data);
				}
				for(var i=1; i<arguments.length; i++){
					str=str.replace(new RegExp("\\{"+(i-1)+"\\}","gi"),arguments[i]);
				}
				return str;
			}
		},
		rootUrl:function(){
			var path="/"+webContextName+"/";
			path=path.replace(/\/+/g,"/")
			return location.protocol+"//"+location.host+path;
		}
}


//========================================================
$(function(){
	//菜单
	$("#js-side-menu li>a").each(function(){
		var link=$(this).attr("href") || "";
		link=link.replace(/\?.*/g,"");
		//选中当前页面菜单
		if(link && location.href.indexOf(link)>=0){
			$(this).parent().addClass("active");
			var childMenu=$(this).closest(".child_menu").show();
			childMenu.parent().addClass("active");
		}
	});

	//设置下拉列表选中项
	$("select[value]").each(function(){
		$("option[value='"+$(this).attr("value")+"']",this).prop("selected",true);
	});
	
	//设置单选/多选按钮选中
	$(":radio,:checkbox").each(function(){
		var _this=$(this);
		_this.prop("checked",_this.attr("check")==_this.val());
	});
	
	//设置文本框只读
	$("input[read='true']").prop("readonly",true);
	
	//日期选择器
	$('.input-date-picker').daterangepicker({
		singleDatePicker: true,
		calender_style: "picker_3",
		format: "YYYY-MM-DD",
		locale : {  
            applyLabel : '确定',  
            cancelLabel : '取消',  
            fromLabel : '起始时间',  
            toLabel : '结束时间',  
            customRangeLabel : '自定义',  
            daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],  
            monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月',  
                    '七月', '八月', '九月', '十月', '十一月', '十二月' ],  
            firstDay : 1  
        }  
   }).prop("readonly",true);
	
	//图片上传
	$(".uploader-img").dropzone({
        url: util.rootUrl()+"/sys/file/uploadProductImg.json",
        maxFiles: 1,
        maxFilesize: 1024*5,
        addRemoveLinks: true,
        dictDefaultMessage: "选择或拖动图片到此上传",
        dictFallbackMessage: "您的浏览器不支持拖动上传",
        dictInvalidFileType: "不允许上传此类型文件",
        dictFileTooBig: "文件超过大小限制",
        dictResponseError: "服务器响应错误：{{statusCode}}",
        dictCancelUpload: "取消",
        dictCancelUploadConfirmation: "确定取消上传？",
        dictMaxFilesExceeded: "只能上传1个文件",
        dictRemoveFile: "取消",
        acceptedFiles: ".jpg,.jpeg,.png,.gif",
        init: function() {
            this.on("success", function(file,result) {
            	if(result && result.result){
                	$(this.element).prev().val(result.data);
            	}
            });
        }
    });
	
	//分页
	if(window["recordTotal"] || window["pageSize"]){
		//总页数
		var totalPage=parseInt(recordTotal/pageSize,10)+(recordTotal%pageSize?1:0);
		//显示开始记录
		var recordFrom=(currPage-1)*pageSize +1;
		recordFrom=recordFrom<=0?1:recordFrom;
		//显示结束记录
		var recordTo=parseInt(recordFrom)+parseInt(pageSize) -1;
		recordTo=recordTo>recordTotal?recordTotal:recordTo;
		
		if(currPage>totalPage){
			recordFrom=0;
			recordTo=0;
			currPage=1;
		}
		
		$("#span-paging-info-from").text(recordFrom);
		$("#span-paging-info-to").text(recordTo);
		$("#span-paging-info-total").text(recordTotal);
		
		$(".table-pagination-bar").pagination({
		  total_pages: totalPage,
		  current_page: currPage,
		  prev: "上一页",
		  next: "下一页",
		  callback: function(event, page) {
			  $("form .js-paging-page").val(page);
			  $("form .js-paging-pagesize").val(pageSize);
			  $("form").submit();
			  return false;
		  }
		});
		$("form")
		.append('<input type="hidden" class="js-paging-page" name="page" value="'+currPage+'"/>')
		.append('<input type="hidden" class="js-paging-pagesize" name="pageSize" value="'+pageSize+'"/>');
	}else{
		$(".table-pagination-info").hide();
	}
	
	/*工具栏*/
	$("#div_list_toolbar a").click(function(){
		var operate=$(this).text();
		var url=$(this).attr("href");
		//需要传参数

		var needParam=/\{\d\}/.test(url);
		var recordId=$(".td_row_id :checkbox:checked").val()
		if(recordId){
			recordId=recordId.split(",");
		}else{
			recordId=[];
		}
		
		if(needParam && recordId.length==0){
			util.el.error("请选择要"+operate+"的记录！");
			return false;
		}
		
		url=util.str.fmt(url, recordId);
		
		if(url.indexOf(".html")>0){
			//页面跳转

			location.href=url;
			return false;
		}else if(url.indexOf(".json")>0){
			//ajax

			util.el.confirm("确认要"+operate+"？",function(){
				$.post(url,function(r){
					if(r && r.result){
						util.el.success(operate+"成功！",location.reload);
					}else{
						util.el.error(operate+"失败！");
					}
				});
			});
			return false;
		}
	});
	
	//===============表单操作处理
	if(!!window["_isFormPost"]){
		//是提交返回的
		var errorList=$("#js-form-error-list");
		if($("li",errorList).length>0){
			errorList.show();
		}else{
			//提交成功弹出提示
			util.el.success("保存");
		}
	}
	
	
});